package com.tusuo.stds.ui.scenesetting;

import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.SceneList;
import com.yydcdut.sdlv.Menu;
import com.yydcdut.sdlv.SlideAndDragListView;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-28.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(UnLockFragment.class)
@EFragment(R.layout.frag_scene_unlock)
public class UnLockFragment extends Fragment {

    @ViewById(R.id.frag_scene_unlock_list)
    SlideAndDragListView mListView;

    private UnLockListAdapter adapter;
    private List<SceneList.Scene> mDataList;
    private SceneList.Scene tempScene;
    private boolean mIsChecked = false;

    @Inject
    ApiHelper mApiHelper;

    @AfterInject
    void initDI() {
        Injector.initUnLockFragmentComponent().inject(this);
    }


    @AfterViews
    void initData() {
        mApiHelper.getSceneList(new GetSceneListSubscriber(),1,"");
    }

    private void initSlideAndDragListView(SceneList sceneList) {
        mDataList = sceneList.Data;

        adapter = new UnLockListAdapter();
        Menu menu = new Menu(false, 0);
        mListView.setMenu(menu);

        mListView.setOnDragDropListener(new SlideAndDragListView.OnDragDropListener() {
            @Override
            public void onDragViewStart(int beginPosition) {
                tempScene = mDataList.get(beginPosition);
            }

            @Override
            public void onDragDropViewMoved(int fromPosition, int toPosition) {
                SceneList.Scene scene = mDataList.remove(fromPosition);
                mDataList.add(toPosition, scene);
            }

            @Override
            public void onDragViewDown(int finalPosition) {
                mDataList.set(finalPosition, tempScene);
                setSceneSort();
              }
        });

        mListView.setOnItemDeleteListener(new SlideAndDragListView.OnItemDeleteListener() {
            @Override
            public void onItemDeleteAnimationFinished(View view, int position) {
                mDataList.remove(position - mListView.getHeaderViewsCount());
                adapter.notifyDataSetChanged();
            }
        });


        mListView.setAdapter(adapter);
        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

    }

    private void setSceneSort() {

        StringBuilder sb = new StringBuilder();
        for (SceneList.Scene scene : mDataList) {
            if (mDataList.indexOf(scene) != mDataList.size() - 1) {
                sb.append(scene.Id + ",");
            }else {
                sb.append(scene.Id);
            }
        }

        mApiHelper.setSceneSortIndex(new SetSceneSortSubscriber(),sb.toString());
    }


    private class UnLockListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

//            View view;
            ViewHolder holder;

            if (convertView == null) {
                convertView = View.inflate(getContext(),R.layout.item_scene_unlock_list,null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) convertView.findViewById(R.id.item_scene_unlock_tv_title);
                holder.ivDrag = (ImageView) convertView.findViewById(R.id.item_scene_unlock_iv_drag);
                holder.rbShow = (CheckBox) convertView.findViewById(R.id.item_scene_unlock_cb_show);
                convertView.setTag(holder);
            }else {
//                view = convertView;
                holder = (ViewHolder) convertView.getTag();
            }

            final SceneList.Scene scene = mDataList.get(position);

            holder.tvTitle.setText(scene.Title);
            holder.ivDrag.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mListView.startDrag(position);
                        }
                    });
                    return true;
                }
            });

            if (scene.IsHidden) {
                holder.rbShow.setChecked(false);
            }else {
                holder.rbShow.setChecked(true);
            }



            holder.rbShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mApiHelper.setSceneHidden(new SceneHiddenSubscriber(),scene.Id,mIsChecked);
                    scene.IsHidden = mIsChecked;
                }
            });

            holder.rbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mIsChecked = !isChecked;
                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView tvTitle;
            CheckBox rbShow;
            ImageView ivDrag;
        }

    }

    private class SceneHiddenSubscriber extends Subscriber<ApiResult> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ApiResult apiResult) {
            System.out.println("apiResult" + apiResult);

        }
    }

    private class GetSceneListSubscriber extends Subscriber<SceneList> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(SceneList sceneList) {
            initSlideAndDragListView(sceneList);
        }
    }

    private class SetSceneSortSubscriber extends Subscriber<ApiResult> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ApiResult apiResult) {


        }
    }

}
